﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Microsoft.WindowsAzure.StorageClient;
using Microsoft.WindowsAzure;

namespace PracticeAzure.Website.Data
{
    public static class BlobHelperLite
    {
        public static BlobRequestOptions DefaultRequestOptions
        {
            get
            {
                return new BlobRequestOptions()
                {
                    Timeout = new TimeSpan(1, 0, 0),
                    RetryPolicy = RetryPolicies.Retry(20, TimeSpan.Zero)
                };
            }
        }

        public static CloudBlobClient GetBlobClient(this CloudStorageAccount account)
        {
            var client = account.CreateCloudBlobClient();
            client.Timeout = new TimeSpan(1, 0, 0);
            client.WriteBlockSizeInBytes = 4 * 1024 * 1024;
            client.RetryPolicy = RetryPolicies.Retry(20, TimeSpan.Zero);
            return client;
        }

        public static CloudBlobContainer GetBlobContainerReference(this CloudStorageAccount account, string containerName, bool createIfNotExist, BlobContainerPermissions permission)
        {
            var client = GetBlobClient(account);
            var container = client.GetContainerReference(containerName);
            if (createIfNotExist)
            {
                container.CreateIfNotExist();
                container.SetPermissions(permission);
            }
            return container;
        }

        public static CloudBlobContainer GetBlobContainerReference(this CloudStorageAccount account, string containerName, bool isPublic)
        {
            var permission = new BlobContainerPermissions()
            {
                PublicAccess = BlobContainerPublicAccessType.Container
            };
            return GetBlobContainerReference(account, containerName, true, permission);
        }
    }
}